<template>
    <h2 style="text-align: center;">医生信息添加</h2>
    <table vocab="1" class="table table-bordered">
        <tbody>
            <tr>
                <th>医生名称</th>
                <th>
                    <input type="text" placeholder="请输入医生名称" v-model="Info.Doctor_Name"/>
                </th>
            </tr>

            <tr>
                <th>所属医院</th>
                <th>
                    <input type="text" placeholder="请输入所属医院" v-model="Info.Doctor_Adress"/>
                </th>
            </tr>

            <tr>
                <th>医生图片</th>
                <th>
                    <img :src="Info.Doctor_Img" style="width: 100px; height: 100px;" />
                    <input type="file" @change="UpLoads"/>
                </th>
            </tr>

            <tr>
                <th>医生学历</th>
                <th>
                    <select v-model="Info.Doctor_Educational">
                        <option value="-1">请选择</option>
                        <option value="博士">博士</option>
                        <option value="硕士">硕士</option>
                        <option value="研究生">研究生</option>
                    </select>
                </th>
            </tr>

            <tr>
                <th>医生状态</th>
                <th>
                    <input type="radio" :value="true" checked v-model="Info.Doctor_IsDelete"/>未删除
                    <input type="radio" :value="false" v-model="Info.Doctor_IsDelete"/>已删除
                </th>
            </tr>

            <tr>
                <th>所属科室</th>
                <th>
                    <select v-model="Info.Dept_Id" >
                        <option :value="-1">请选择</option>
                        <option v-for="item in InfoDept" :value="item.Dept_Id">{{ item.Dept_Name }}</option>
                    </select>
                </th>
            </tr>

            <tr>
                <th>职称</th>
                <th>
                    <select v-model="Info.Professional_Id" >
                        <option :value="-1">请选择</option>
                        <option v-for="item in InfoPro" :value="item.Professional_Id">{{ item.Professional_Name }}</option>
                    </select>
                </th>
            </tr>

            <tr>
                <th colspan="2" style="text-align: center;">
                    <input  type="button" value="添加医生信息" class="btn btn-success" @click="AddDoctor" style="margin-right: 10px;"/>
                    <a href="ShowDoctor">返回列表</a>
                </th>
            </tr>
        </tbody>
    </table>
</template>


<script setup lang="ts">
    import {ref,onMounted} from 'vue';
    import axios from 'axios';
    import { useRouter } from 'vue-router';
    import { useCounterStore } from '@/stores/counter';

    const router=useRouter();
    const store=useCounterStore();
    if(store.Userid=="")
    {
        alert('请选登录');
        router.push("/Login");
    }
    
    //添加响应式数据
    const Info=ref({
        Doctor_Name: "",
        Doctor_Adress: "",
        Doctor_Img: "",
        Doctor_Educational: -1,
        Doctor_IsDelete: true,
        Dept_Id: -1,
        Professional_Id: -1
    })
    //职称响应式数据
    const InfoPro=ref([{
        Professional_Id: 0,
        Professional_Name: ""
    }])
    //获取科室响应式数据
    const InfoDept=ref([{
        Dept_Id:0,
        Dept_Name:""
    }])
    //文档就绪函数
    onMounted(()=>{
        GetDepartments();//获取科室信息
        GetProfessionals();//获取职称信息
    })
    //获取科室信息
    const GetDepartments=()=>{
        axios.get("https://localhost:44306/api/Ward/GetDepartments")
        .then(res=>{
            InfoDept.value=res.data
        })
    }
    //获取职称信息
    const GetProfessionals=()=>{
        axios.get("https://localhost:44306/api/Doctor/GetProfessionals")
        .then(res=>{
            InfoPro.value=res.data
        })
    }
    //图片上传 
    const UpLoads=(img:any)=>{
        var file=img.target.files[0];
        var fd=new FormData();
        fd.append("img",file);
        axios.post("https://localhost:44306/api/FilesUpLoads/UpLoads",fd)
        .then(res=>{
            console.log(res.data);
            if(res.data.Coom_Code==-1)
            {
                alert(res.data.Coom_Msg);
            }
            else if(res.data.Coom_Code==-2)
            {
                alert(res.data.Coom_Msg);
            }
            else
            {
                Info.value.Doctor_Img=res.data.Coom_Path
            }
        })
    }
    //添加医生信息
    const AddDoctor=()=>{
        //非空验证
        if(Info.value.Doctor_Name=="")
        {
            alert('医生名称不能为空');
            return;
        }
        axios.post("https://localhost:44306/api/Doctor/AddDoctor",Info.value)
        .then(res=>{
            if(res.data>0)
            {
                alert('医生信息添加成功');
                router.push("/ShowDoctor");//跳转到显示页面
            }
            else
            {
                alert('医生信息添加失败');
            }
        })
    }
</script>